package com.zeny.xdclass.utils;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName HttpUtils
 * @Description TODO
 * @Author zeny
 */
@Slf4j
public class HttpUtils {

    private static final Gson gson = new Gson();

    /**
     * @Description get方法
     * @param url
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/
    public static Map<String, Object> doGet(String url) {
        Map<String, Object> map = new HashMap<>();
        CloseableHttpClient httpClient = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000) //连接超时
                .setConnectionRequestTimeout(5000)  //请求超时
                .setSocketTimeout(5000)
                .setRedirectsEnabled(true) //允许重定向
                .build();
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(requestConfig);
        try {
            HttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                String jsonResult = EntityUtils.toString(response.getEntity());
                map = gson.fromJson(jsonResult, Map.class);
            }
        }catch (Exception e) {
            log.error(e.getStackTrace().toString());
        }finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                log.error(e.getStackTrace().toString());
            }
        }
        return map;
    }

    /**
     * @Description post方法
     * @param url
     * @return java.lang.String
     **/
    public static String doPost(String url, String data) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000) //连接超时
                .setConnectionRequestTimeout(5000)  //请求超时
                .setSocketTimeout(5000)
                .setRedirectsEnabled(true) //允许重定向
                .build();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(requestConfig);
        httpPost.addHeader("Content-Type", "text/html;chartset=utf-8");
        if (!StringUtils.isEmpty(data)) {
            StringEntity stringEntity = new StringEntity(data, "UTF-8");
            httpPost.setEntity(stringEntity);
        }
        try {
            CloseableHttpResponse response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == 200) {
                String result = EntityUtils.toString(response.getEntity());
                 return result;
            }
        }catch (Exception e) {
            log.error(e.getStackTrace().toString());
        }finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                log.error(e.getStackTrace().toString());
            }
        }
        return null;
    }
}
